﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NUnit.Framework;
using CondominioG1.Persistencia.NHibernate;
using CondominioG1.Dominio;
using CondominioG1.Negocio;

using Spring.Context;
using Spring.Context.Support;


namespace CondominioG1.Pruebas.TestService
{
            [TestFixture]

    public class TestCuotaService
    {


        private IApplicationContext Spring;

        [TestFixtureSetUp]
        public void InicializarPruebas()
        {
            //"assembly://assembly/namespace/capaPersistencia.xml",
            Spring = new XmlApplicationContext(
                "assembly://CondominioG1/CondominioG1/capaPersistencia.xml",
                "assembly://CondominioG1/CondominioG1/capaNegocio.xml"
                );

            // Obtengo una referencia a la clase de negocio a probar
            ReservaService reservaService = (ReservaService)Spring.GetObject("reservaService");
            ViviendaService viviendaService = (ViviendaService)Spring.GetObject("viviendaService");
            ResidenteService residenteService = (ResidenteService)Spring.GetObject("residenteService");
            CuotaService cuotaService = (CuotaService)Spring.GetObject("cuotaService");

            //Limpia la base de datos de Condominio
            reservaService.EliminarReservas();
            cuotaService.EliminaCuotas();
            viviendaService.EliminarViviendas();
            residenteService.EliminarResidentes();
 
            //Data para modificar y eliminar usada en las pruebas
            residenteService.RegistrarResidente("71255612", "Logan", "Castillo", 31, "lcastillo@claro.com", "logan", "PROPIETARIO");
            viviendaService.RegistrarVivienda("V0001", "A51", 1023, 125, "DUPLEX", "71255612", "71255612");
            cuotaService.FijarCuota("V0001","JUL2011",580, DateTime.Parse("30/07/2011"),false,DateTime.Parse("30/07/2011"),"");
            cuotaService.FijarCuota("V0001", "AGO2011",580, DateTime.Parse("30/07/2011"), false, DateTime.Parse("30/07/2011"), "");
        }

        [Test]
        public void TestFijarCuota_correctamente()
        {
            CuotaService cuotaService = (CuotaService)Spring.GetObject("cuotaService");
            Cuota cuota= null;
            Assert.DoesNotThrow(delegate {
                cuota= cuotaService.FijarCuota("V0001","JUN2011",580, DateTime.Parse("30/08/2011"),false,DateTime.Parse("30/08/2011"),"contado");
            });
            Assert.NotNull(cuota);
            Assert.Greater(cuota.Importe, 0);
        }

        [Test]
        public void TestFijarCuota_incorrectamente()
        {
            CuotaService CuotaService = (CuotaService)Spring.GetObject("cuotaService");
            Cuota cuotaCreado = null;
            // Pruebas solicitadas
            Assert.Catch<Exception>(delegate {
                cuotaCreado = CuotaService.FijarCuota("V0001", "JUL2011", 580, DateTime.Parse("30/07/2011"), false, DateTime.Parse("30/07/2011"), "contado");
            });
        }

        [Test]
        public void TestPagarCuota()
        {
            CuotaService cuotaService = (CuotaService)Spring.GetObject("cuotaService");
            Assert.NotNull(cuotaService);
            Cuota cuota = null;
            Assert.DoesNotThrow(delegate
            {
                cuota = cuotaService.PagarCuota("V0001", "AGO2011", "CONTADO");
            });
        }

        [Test]
        public void TestCuotasPorPagar()
        {
            CuotaService cuotaService = (CuotaService)Spring.GetObject("cuotaService");
            ICollection<Cuota> LCuota = cuotaService.ListaCuotasPorPagar();
            Assert.NotNull(LCuota);
        }
}
}